/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.pneu.pneuservisbackend.dao;

import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import pa165.pneu.pneuservisbackend.entiies.ServicePneu;

/**
 *
 * @author Lukáš Valkovic
 */
@Repository
public class ServicePneuDAOImpl implements ServicePneuDAO{
    
    @PersistenceContext
    private EntityManager entityManager;
    
    @Override
    public void createService(ServicePneu servicePneu) throws IllegalArgumentException{
        if (servicePneu == null) {
            throw new IllegalArgumentException("Service can't be null.");
        }

        if (servicePneu.getId() != null) {
            throw new IllegalArgumentException("Service ID is already set.");
        }
        
        validate(servicePneu);
         
       
        
        entityManager.persist(servicePneu);
        
    }

    @Override
    public ServicePneu getService(Long id) throws IllegalArgumentException{
        if (id == null) {
            throw new IllegalArgumentException("ID is null.");
        }
        
        
        
        ServicePneu servicePneu = entityManager.find(ServicePneu.class, id);
        entityManager.detach(servicePneu);
        
        
        
        return servicePneu;
    }

    @Override
    public void updateService(ServicePneu servicePneu) throws IllegalArgumentException{
        if (servicePneu == null) {
            throw new IllegalArgumentException("Service can't be null.");
        }
        if (servicePneu.getId() == null) {
            throw new IllegalArgumentException("You can't update service with null ID");
        }
        
        validate(servicePneu);
        
        
        
        ServicePneu result = entityManager.find(ServicePneu.class, servicePneu.getId());
        
        
        if (result == null) {
            
            throw new IllegalArgumentException("Service {0} is not in database." + servicePneu);
        } 
//        if (!servicePneu.equals(result)) {
//            
//            throw new IllegalArgumentException("Niekde sa stala chyba");
//        }
        
        
        entityManager.merge(result);
        
        
    }

    @Override
    public void removeService(ServicePneu servicePneu) throws IllegalArgumentException{
        if (servicePneu == null) {
            throw new IllegalArgumentException("Service can't be null.");
        }
        if (servicePneu.getId() == null) {
            throw new IllegalArgumentException("You can't remove service with null ID");
        }
        
        
        
        ServicePneu result = entityManager.find(ServicePneu.class, servicePneu.getId());
        
        
        if (result == null) {
            
            throw new IllegalArgumentException("Service {0} is not in database." + servicePneu);
        }
        
//        if (!servicePneu.equals(result)) {
//            
//            throw new IllegalArgumentException("Niekde sa stala chyba");
//        }

        
        entityManager.remove(result);
        
        
    }

    @Override
    public List<ServicePneu> getAllServices() {
        
        List<ServicePneu> resultList;
        Query query = entityManager.createQuery("SELECT s FROM ServicePneu s ORDER BY s.id");
        
        resultList = query.getResultList();
        entityManager.detach(resultList);
        
        
        return resultList;
    }

    @Override
    public List<ServicePneu> findServiceWithName(String name) throws IllegalArgumentException{
        if (name == null) {
            throw new IllegalArgumentException("You can't remove service with null ID");
        }
        
        
        List<ServicePneu> resultList;
            Query query = entityManager.createQuery("SELECT s FROM ServicePneu s WHERE s.name LIKE :name ORDER BY s.id");
        query.setParameter("name", name);
        
        resultList = query.getResultList();
        entityManager.detach(resultList);
        
        
        return resultList;
    }
    
    private void validate(ServicePneu servicePneu) {
        if (servicePneu == null) {
            throw new IllegalArgumentException("servise can't be null.");
        }
        if (servicePneu.getName() == null) {
            throw new IllegalArgumentException("Name can't be null.");
        }
        if ("".equals(servicePneu.getName())) {
            throw new IllegalArgumentException("Name with wrong attribute.");
        }
        if (" ".equals(servicePneu.getName())) {
            throw new IllegalArgumentException("Wrong name attribute.");
        }
        if (servicePneu.getPrice() == null) {
            throw new IllegalArgumentException("Price can't be null.");
        }
        if (servicePneu.getPrice().compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException("Price can't be negative or 0.");
        }
    }
}
